package com.erp.auto.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.erp.auto.domain.ErpAutoPriceOffProductProject;
import com.erp.auto.domain.ErpAutoPriceOffProductProjectTask;
import com.erp.auto.domain.bo.ErpAutoPriceOffProductProjectBo;
import com.erp.auto.mapper.ErpAutoPriceOffProductProjectMapper;
import com.erp.auto.mapper.ErpAutoPriceOffProductProjectTaskMapper;
import com.erp.auto.service.ErpAutoPriceOffProductProjectService;
import com.ruoyi.common.core.domain.R;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

@Slf4j
@RequiredArgsConstructor
@Service
public class ErpAutoPriceOffProductProjectServiceImpl implements ErpAutoPriceOffProductProjectService {
    @Resource
    RestTemplate restTemplate;
    private final String erpConfigStr = "http://localhost:8860";
    private final ErpAutoPriceOffProductProjectMapper baseMapper;
    private final ErpAutoPriceOffProductProjectTaskMapper erpAutoPriceOffProductProjectTaskMapper;

    @Override
    public R<Object> addProject(ErpAutoPriceOffProductProjectBo projectBo) {
        ErpAutoPriceOffProductProject add = BeanUtil.toBean(projectBo, ErpAutoPriceOffProductProject.class);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            projectBo.setId(add.getId());
            return R.ok("添加成功");
        } else {
            return R.fail("添加失败");
        }
    }

    @Override
    public R<Object> queryProjectById(String id) {
        ErpAutoPriceOffProductProject project = baseMapper.selectById(id);
        if (Objects.isNull(project)) {
            return R.fail("计划不存在");
        }
        return R.ok(project);
    }

    @Override
    public R<Object> editProject(ErpAutoPriceOffProductProjectBo projectBo) {
        ErpAutoPriceOffProductProject edit = BeanUtil.toBean(projectBo, ErpAutoPriceOffProductProject.class);
        boolean flag = baseMapper.updateById(edit) > 0;
        if (flag) {
            projectBo.setId(edit.getId());
            return R.ok("修改成功");
        } else {
            return R.fail("修改失败");
        }
    }

    @Override
    public R<Object> editProjectStatus(String id, Integer status) {
        ErpAutoPriceOffProductProject edit = baseMapper.selectById(id);
        if (Objects.isNull(edit)) {
            return R.fail("计划不存在");
        }
        edit.setStatus(status);
        if (baseMapper.updateById(edit) > 0) {
            return R.ok("修改状态成功");
        } else {
            return R.fail("修改状态失败");
        }
    }

    /**
     * 定时任务测试
     *
     * @param id
     * @return
     */
    @Override
    public R<Object> runJobTest(Long id) {
        List<ErpAutoPriceOffProductProject> projectList = baseMapper.selectList(
                new LambdaQueryWrapper<ErpAutoPriceOffProductProject>().eq(ErpAutoPriceOffProductProject::getStatus, 1)
        );

        if (ObjectUtils.isNotNull(id)) {
            projectList = baseMapper.selectList(
                    new LambdaQueryWrapper<ErpAutoPriceOffProductProject>().eq(ErpAutoPriceOffProductProject::getId, id)
            );
        }

        for (ErpAutoPriceOffProductProject project : projectList) {
//            ResponseEntity<String> forEntity = restTemplate
//                    .postForEntity(erpConfig.getEndpoint() + "/gather/autoPriceOffProductProject/queryNeedPriceOffProductList", project, String.class);
//            String body = (String) forEntity.getBody();
//            System.out.println(body);
//            String data = JSONObject.parseObject(body).getString("data");
//            List<ErpAutoPriceOffProductProjectTask> taskList = JSONArray.parseArray(data, ErpAutoPriceOffProductProjectTask.class);
//            taskList.forEach(
//                    task -> {
//                        erpAutoPriceOffProductProjectTaskMapper.insert(task);
//                    }
//            );
        }


        return null;
    }

    /**
     * 模拟定时任务 二号，跑符合降价要求的任务
     *
     * @param id
     * @return
     */
    @Override
    public R<Object> runJobTestTwo(Long id) {
        List<ErpAutoPriceOffProductProjectTask> taskList = erpAutoPriceOffProductProjectTaskMapper.selectList(
                new LambdaQueryWrapper<ErpAutoPriceOffProductProjectTask>().eq(ErpAutoPriceOffProductProjectTask::getStatus, 3)
        );
        if (ObjectUtils.isNotNull(id)) {
            taskList = erpAutoPriceOffProductProjectTaskMapper.selectList(
                    new LambdaQueryWrapper<ErpAutoPriceOffProductProjectTask>().eq(ErpAutoPriceOffProductProjectTask::getId, id)
            );
        }

        for (ErpAutoPriceOffProductProjectTask task : taskList) {
            //设置为调价中
            task.setStatus(4);
            erpAutoPriceOffProductProjectTaskMapper.updateById(task);

            ErpAutoPriceOffProductProject project = baseMapper.selectById(task.getProjectId());
            Map<String, Object> map = new HashMap<>();
            map.put("priceOffRange", project.getPriceOffRange());
            //2、降价预期方式
            map.put("priceOffExpectType", project.getPriceOffExpectType());
            //降价预期
            map.put("priceOffExpect", project.getPriceOffExpect());
            map.put("aeProductId", task.getAeProductId());
            map.put("skuCode", task.getSkuCode());
            map.put("orderCountry", task.getOrderCountry());

//            ResponseEntity<String> forEntity = restTemplate
//                    .postForEntity(erpConfig.getEndpoint() + "/gather/autoPriceOffProductProject/priceOffProductList", map, String.class);
//            String body = (String) forEntity.getBody();
//            System.out.println(body);
//            Integer code = JSONObject.parseObject(body).getInteger("code");
//            String msg = JSONObject.parseObject(body).getString("msg");
//            if (code == 200) {
//                //降价成功
//                task.setStatus(5);
//                task.setRemark("调价成功");
//                erpAutoPriceOffProductProjectTaskMapper.updateById(task);
//            } else {
//                //降价失败
//                task.setStatus(6);
//                //设置备注调价失败
//                task.setRemark(msg);
//                erpAutoPriceOffProductProjectTaskMapper.updateById(task);
//            }
        }
        return R.ok("降价任务跑完");
    }

    @Override
    public Object getProjectTaskList(String projectId, String status) {
        return null;
    }

}
